/*
 * @Author: cl
 * @Description: 
 * @Date: 2023-05-08 14:34:26
 * @LastEditTime: 2023-05-09 11:38:08
 * @FilePath: \jsnode\nestjs\naota\src\prismasql\prismasql.service.ts
 * @LastEditors: cl
 */
import { Injectable } from '@nestjs/common';
import { CreatePrismasqlDto } from './dto/create-prismasql.dto';
import { UpdatePrismasqlDto } from './dto/update-prismasql.dto';
import { PrismaService } from '@/prisma/prisma.service';
import { log } from 'console';

@Injectable()
export class PrismasqlService{

  constructor(private prisma:PrismaService){
  }

  create(createPrismasqlDto: CreatePrismasqlDto) {
    return 'This action adds a new prismasql';
  }

  async findAll() {
    let list = await this.prisma.article.findMany({
      include:{
        category:true
      }
    })
    let rDate = []
    list.forEach((item)=>{
      // item = {...this.addProperty(item,'categoryName',item.category.title)}
      rDate.push({...this.addProperty(item,'categoryName',item.category.title)})
    })
    console.log(rDate);
    rDate.forEach((item)=>{
      delete item.category
    })
    return rDate;
  }

  addProperty(object, name, fn) {
    return {
        ...object,
        [name]: fn,
    }
}

  findOne(id: number) {
    return `This action returns a #${id} prismasql`;
  }

  update(id: number, updatePrismasqlDto: UpdatePrismasqlDto) {
    return `This action updates a #${id} prismasql`;
  }

  remove(id: number) {
    return `This action removes a #${id} prismasql`;
  }
}
